﻿// ----------------------------------------------------------------------------------
// Sample Code from Fernando Machado Píriz's blog: http://fernandomachadopiriz.com
//
// <copyright company="Fernando Machado Píriz" file="desktopBackgroundModel.cs">
// Copyright (c) Fernando Machado Píriz. Distributed under FSF's GPL v2.</copyright>
// ----------------------------------------------------------------------------------
// This code and information are provided "as is" without warranty of any kind,
// either expressed of implied, including but not limited to the implied warranties
// of merchantability and/or fitness for a particular purpose.
// ----------------------------------------------------------------------------------
namespace DesktopBackground
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Reflection;
    using System.Text;
    using GivingAPresentation.Commons;
    
    /// <summary>
    /// The model of the <code>DesktopBackgroundView</code> using the Model View View Model pattern.
    /// </summary>
    public class DesktopBackgroundModel : BaseModel
    {
        /// <summary>
        /// The name of the setting exposed in this model.
        /// </summary>
        private const string WallpaperSettingName = "Wallpaper";

        /// <summary>
        /// The name of the setting exposed in this model.
        /// </summary>
        private const string TileWallpaperSettingName = "TileWallpaper";

        /// <summary>
        /// The name of the setting exposed in this model.
        /// </summary>
        private const string WallpaperStyleSettingName = "WallpaperStyle";

        /// <summary>
        /// The name of the setting exposed in this model.
        /// </summary>
        private const string ReplaceDesktopBackgroundSettingName = "ReplaceDesktopBackground";

        /// <summary>
        /// The name of the setting exposed in this model.
        /// </summary>
        private const string RemoveDesktopBackgroundSettingName = "RemoveDesktopBackground";

        /// <summary>
        /// The backend field for the <code>Wallpaper</code> property.
        /// </summary>
        private string wallpaper;

        /// <summary>
        /// The backend field for the <code>TileWallpaper</code> property.
        /// </summary>
        private int tileWallpaper;

        /// <summary>
        /// The backend field for the <code>WallpaperStyle</code> property.
        /// </summary>
        private int wallpaperStyle;

        /// <summary>
        /// The backend field for the <code>Remove</code> property.
        /// </summary>
        private bool remove;

        /// <summary>
        /// The backend field for the <code>Replace</code> property.
        /// </summary>
        private bool replace;

        /// <summary>
        /// Gets or sets the value of the user selected desktop background tiled configuration (1: tiled; 0: center or fit to screen).
        /// </summary>
        public int TileWallpaper
        {
            get
            {
                return this.tileWallpaper;
            }

            set
            {
                this.tileWallpaper = value;
                this.NotifyPropertyChanged("Position");
            }
        }

        /// <summary>
        /// Gets or sets the value of the user selected desktop background stye configuration (1: centered; 0: tiled; 2: fit to screen).
        /// </summary>
        public int WallpaperStyle
        {
            get
            {
                return this.wallpaperStyle;
            }

            set
            {
                this.wallpaperStyle = value;
                this.NotifyPropertyChanged("Position");
            }
        }

        /// <summary>
        /// Gets or sets the path to user selected desktop background file.
        /// </summary>
        public string Wallpaper
        {
            get
            {
                return this.wallpaper;
            }

            set
            {
                this.wallpaper = value;
                this.NotifyPropertyChanged("Wallpaper");
            }
        }

        /// <summary>
        /// Gets or sets the position of the selected desktop background file:
        ///   0: Centered
        ///   1: Tiled
        ///   2: Fit to screen
        /// </summary>
        public int Position
        {
            get
            {
                if (this.tileWallpaper == 0 && this.wallpaperStyle == 1)
                {
                    return 0;
                }
                else if (this.tileWallpaper == 1 && this.wallpaperStyle == 0)
                {
                    return 1;
                }
                else
                {
                    this.tileWallpaper = 0;
                    this.wallpaperStyle = 2;
                    return 2;
                }
            }

            set
            {               
                if (value == 0)
                {
                    this.tileWallpaper = 0;
                    this.wallpaperStyle = 1;
                }
                else if (value == 1)
                {
                    this.tileWallpaper = 1;
                    this.wallpaperStyle = 0;
                }
                else if (value == 2)
                {
                    this.tileWallpaper = 0;
                    this.wallpaperStyle = 2;
                }

                this.NotifyPropertyChanged("Position");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the desktop background should be removed while the user is giving a presentation.
        /// </summary>
        public bool Remove
        {
            get
            {
                return this.remove;
            }

            set
            {
                this.remove = value;
                if (this.remove)
                {
                    this.Replace = false;
                }

                this.NotifyPropertyChanged("Remove");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the desktop background should be replaced by the file pointed by <code>Wallpaper</code> while
        /// the user is giving a presentation.
        /// </summary>
        public bool Replace
        {
            get
            {
                return this.replace;
            }

            set
            {
                this.replace = value;
                if (this.replace)
                {
                    this.Remove = false;
                }

                this.NotifyPropertyChanged("Replace");
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the user is giving a presentation.
        /// </summary>
        public bool Presenting { get; set; }

        /// <summary>
        /// Load model state using the <code>SettingsManager</code>.
        /// </summary>
        public void Load()
        {
            this.Wallpaper = SettingsManager.Instance.GetValue(WallpaperSettingName, String.Empty);

            // Since Position depends on certain specific combination of TileWallpaper and WallpaperStyle values, we cannot assign both at the
            // same time with a valid combination of values; any assignment through the TileWallpaper or WallpaperStyle always reset Position
            // to 2 (fit to screen). Values are read to backend properties and then a fake Position assignment is made to fire the change
            // notification.
            this.tileWallpaper = SettingsManager.Instance.GetValue(TileWallpaperSettingName, 0);
            this.wallpaperStyle = SettingsManager.Instance.GetValue(WallpaperStyleSettingName, 2);
            this.Position = this.Position;

            this.Remove = SettingsManager.Instance.GetValue(RemoveDesktopBackgroundSettingName, true);
            this.Replace = SettingsManager.Instance.GetValue(ReplaceDesktopBackgroundSettingName, false);
        }

        /// <summary>
        /// Saves model state using the <code>SettingsManager</code>.
        /// </summary>
        public void Save()
        {
             SettingsManager.Instance.SetValue(WallpaperSettingName, this.Wallpaper);
             SettingsManager.Instance.SetValue(TileWallpaperSettingName, this.TileWallpaper);
             SettingsManager.Instance.SetValue(WallpaperStyleSettingName, this.WallpaperStyle);
             SettingsManager.Instance.SetValue(RemoveDesktopBackgroundSettingName, this.Remove);
             SettingsManager.Instance.SetValue(ReplaceDesktopBackgroundSettingName, this.Replace);
        }
    }
}
